import {
  userhealthyList,
  delHealthy
} from '../../../utils/api'
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list: [],
    isNull: false,
    max: 6,
    imgUrl: app.globalData.imgUrl,
    wxImgUrl: app.globalData.wxImgUrl,
    navBarHeight: '', //头高度
    statusBarHeight: '', //状态栏高度
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let navBarHeightinfo = app.publicFun.navBarHeightinfo()
    this.setData({
      navBarHeight: navBarHeightinfo[0],
      statusBarHeight: navBarHeightinfo[1]
    })
  },
  back() {
    wx.navigateBack({})
  },
  onShow() {
    this.getlist()
  },
  getlist() {
    userhealthyList({}).then(res => {
      console.log(res)
      if (res.length < 6) {
        this.setData({
          max: 5
        })
      } else {
        this.setData({
          max: 6
        })
      }
      this.setData({
        list: res
      })
    })
  },
  handlerAdd() {
    wx.navigateTo({
      url: '../addhealthy/index',
    })
  },
  handlerMore(e) {
    let i = e.currentTarget.dataset.id;
    if (i == 0) {
      this.data.max = this.data.list.length
    } else {
      this.data.max = 6
    }
    this.setData({
      max: this.data.max
    })
  },
  handlerDel(e) {
    let id = e.currentTarget.dataset.id;
    delHealthy({
      id: id
    }).then(res => {
      if (res.code == undefined) {
        wx.showToast({
          title: '删除成功',
          icon: 'none'
        })
        this.getlist()
      } else {
        wx.showToast({
          title: res.message,
          icon: 'none'
        })
      }
    })
  }
})